<template>
  <el-dialog
    :title="isEdit ? '编辑考核组成' : '添加考核组成'"
    :model-value="visible"
    @update:model-value="$emit('update:visible', $event)"
    width="500px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="formRules"
      label-width="120px"
    >
      <el-form-item label="组成名称" prop="name">
        <el-input 
          v-model="form.name" 
          placeholder="请输入考核组成名称"
          maxlength="50"
          show-word-limit
        />
      </el-form-item>
      
      <el-form-item label="比例" prop="proportion">
        <el-input-number
          v-model="form.proportion"
          :min="0"
          :max="100"
          :precision="1"
          controls-position="right"
          style="width: 100%;"
        />
        <span class="form-tip">%</span>
      </el-form-item>
      
      <el-form-item label="计分方式" prop="scoreTypeId">
        <el-select 
          v-model="form.scoreTypeId" 
          placeholder="选择计分方式"
          style="width: 100%;"
        >
          <el-option
            v-for="scoreType in scoreTypes"
            :key="scoreType.id"
            :label="scoreType.name"
            :value="scoreType.id"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="考核类型" prop="examPartType">
        <el-select v-model="form.examPartType" placeholder="请选择考核类型">
          <el-option label="形成性考核" value="FORMATIVE" />
          <el-option label="终结性考核" value="FINAL" />
          <el-option label="其它" value="OTHER" />
        </el-select>
        <span class="form-tip">选择考核的性质类型</span>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">
        {{ isEdit ? '更新' : '创建' }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  examPart: {
    type: Object,
    default: () => ({})
  },
  scoreTypes: {
    type: Array,
    default: () => []
  },
  isEdit: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:visible', 'submit'])

const formRef = ref()
const submitting = ref(false)

const form = reactive({
  name: '',
  proportion: null,
  scoreTypeId: null,
  examPartType: 'FORMATIVE',
  teachingMissionId: null
})

const formRules = {
  name: [
    { required: true, message: '请输入考核组成名称', trigger: 'blur' },
    { min: 2, max: 50, message: '名称长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  proportion: [
    { required: true, message: '请输入比例', trigger: 'blur' },
    { type: 'number', min: 0, max: 100, message: '比例必须在0-100之间', trigger: 'blur' }
  ],
  scoreTypeId: [
    { required: true, message: '请选择计分方式', trigger: 'change' }
  ]
}

// 监听外部数据变化
watch(() => props.examPart, (newExamPart) => {
  if (newExamPart && Object.keys(newExamPart).length > 0) {
    Object.assign(form, {
      name: newExamPart.name || '',
      proportion: newExamPart.proportion || null,
      scoreTypeId: newExamPart.scoreTypeId || null,
      examPartType: newExamPart.examPartType || 
                   (newExamPart.isFormative ? 'FORMATIVE' : 
                    newExamPart.isFinal ? 'FINAL' : 'FORMATIVE'),
      teachingMissionId: newExamPart.teachingMissionId || null
    })
  } else {
    // 重置表单
    Object.assign(form, {
      name: '',
      proportion: null,
      scoreTypeId: null,
      examPartType: 'FORMATIVE',
      teachingMissionId: null
    })
  }
}, { immediate: true, deep: true })

const handleClose = () => {
  emit('update:visible', false)
}

const handleCancel = () => {
  handleClose()
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    submitting.value = true
    emit('submit', { ...form })
    handleClose()
  } catch (error) {
    // 表单验证失败，不进行提交
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped>
.form-tip {
  margin-left: 8px;
  color: #909399;
  font-size: 12px;
}
</style>
